<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/10/12
 * Time: 10:37
 */

namespace app\admin\controller;

use app\admin\service\PermissionService;
use app\admin\validator\PermissionValidator;
use lib\common\CommonController;
use lib\annotations\Permission;

class Permissions extends CommonController
{
    /**
     * @Permission
     * @param PermissionService $service
     * @return void
     */
    public function index (PermissionService $service): void {
        $params = $this->request->all();
        if (isset($params['all'])) {
            $data = $service->getAll();
        } else {
            $data = $service->getList($params);
        }
        $this->success($data);
    }

    /**
     * @Permission
     * @param PermissionService $service
     * @return void
     */
    public function create (PermissionService $service): void {
        $params = $this->request->all();
        (new PermissionValidator())->goCheck($params, 'create');
        $service->create($params);
        $this->success();
    }

    /**
     * @Permission
     * @param $id
     * @param PermissionService $service
     * @return void
     */
    public function update ($id, PermissionService $service): void {
        $params = $this->request->all();
        (new PermissionValidator())->goCheck($params, 'create');
        $service->update($id, $params);
        $this->success();
    }

    /**
     * @Permission
     * @param $id
     * @param PermissionService $service
     * @return void
     */
    public function read ($id, PermissionService $service): void {
        $data = $service->read($id);
        $this->success($data);
    }

    /**
     * @Permission
     * @param $id
     * @param PermissionService $service
     * @return void
     */
    public function delete ($id, PermissionService $service): void {
        $service->delete($id);
        $this->success();
    }
}
